package com.msb;

import java.awt.*;
import java.util.Random;

/// 坦克类
public class Tank {
    // 坐标
    private int x, y;

    // 方向
    private MyDir dir;

    // 速率
    private static final int speed = 5;

    // 持有窗口类
    private GameFrame frame;

    // 分组
    private Group group = Group.Good;

    // 宽高
    public int width = ResourceMgr.goodTankL.getWidth(), height = ResourceMgr.goodTankL.getHeight();

    // 移动
    private boolean moving = true;

    // 随机开火
    private Random random = new Random();

    // 存活
    private boolean isLiving = true;

    // 矩形
    public Rectangle rect = new Rectangle(x, y, width, height);

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Tank(int x, int y, MyDir dir, GameFrame frame, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.frame = frame;
        this.group = group;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public int getWidth() {
        switch (dir) {
            case UP:
                width = ResourceMgr.goodTankU.getWidth();
                break;
            case DOWN:
                width = ResourceMgr.goodTankD.getWidth();
                break;
            case LEFT:
                width = ResourceMgr.goodTankL.getWidth();
                break;
            case RIGHT:
                width = ResourceMgr.goodTankR.getWidth();
                break;
        }

        return width;
    }

    public  int getHeight() {
        switch (dir) {
            case UP:
                width = ResourceMgr.goodTankU.getHeight();
                break;
            case DOWN:
                width = ResourceMgr.goodTankD.getHeight();
                break;
            case LEFT:
                width = ResourceMgr.goodTankL.getHeight();
                break;
            case RIGHT:
                width = ResourceMgr.goodTankR.getHeight();
                break;
        }
        return height;
    }

    // 画坦克
    public void paint(Graphics g) {

        // 坦克不存活了，移除
        if (!isLiving) frame.tanks.remove(this);

        switch (dir) {
            case UP:
                g.drawImage(this.group == Group.Good ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y,null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.Good ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y,null);
                break;
            case LEFT:
                g.drawImage(this.group == Group.Good ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y,null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.Good ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y,null);
                break;
        }
    }

    // 设置坦克方向,并且移动坦克
    public void setDir(MyDir dir) {
        this.dir = dir;
        move();
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public boolean isMoving() {
        return moving;
    }

    /// 移动坦克
    public void move() {

        if (!moving) {return;}

        switch (dir) {
            case DOWN:
                y += speed;
                break;
            case UP:
                y -= speed;
                break;
            case LEFT:
                x -= speed;
                break;
            case RIGHT:
                x += speed;
                break;
            default:
        }

        // 敌军随机发送子弹
        if (this.group == Group.Bad && random.nextInt(10) > 8)
            this.fire();

        // 敌军随机改变方向
        if (this.group == Group.Bad && random.nextInt(100) > 95)
            randDirection();

        boundsCheck();

        rect.x = x;
        rect.y = y;
    }

    // 边界检测
    private void boundsCheck() {
        if (this.x < 0) x = 0;
        if (this.y < 30) y = 30;
        if (this.x > GameFrame.GAME_WIDTH - getWidth()) x = GameFrame.GAME_WIDTH - getWidth();
        if (this.y > GameFrame.GAME_HEIGHT - getHeight()) y = GameFrame.GAME_HEIGHT - getHeight();
    }

    // 随机方向
    private void randDirection() {
        this.dir = MyDir.values()[random.nextInt(4)];
    }

    /// 开火
    public void fire() {

        this.frame.bullets
                .add(new Bullet(x + width/2 - Bullet.width / 2,y + height/2 - Bullet.height / 2, dir, frame, this.group));
    }

    /// 坦克死亡
    public void die() {
        isLiving = false;
    }
}
